﻿using System.Linq;
using BingoClientModel;
using BingoServerCommon.Common;
using Dapper;
using MySql.Data.MySqlClient;


namespace BingoHallServerConsole.Business.Account
{
	/// <inheritdoc />
	public class CS_GetAchievements : Package
	{


		public CS_GetAchievements(EnumPackCommand mainid) : base(mainid)
		{
		}

		public CS_GetAchievements(byte[] buffer, int len) : base(buffer, len)
		{
		}

		public override Package Clone(byte[] bytes)
		{
			return new CS_GetAchievements(bytes, bytes.Length);
		}



		/// <summary>
		/// 得到用户成就
		/// </summary>
		public override void Excute(Session session)
		{
			var playerId = ReadInt();

			MySqlConnection conn = Program.ConnectionPool.getConnection(); //从连接池拿到一个连接
			string sql = $"SELECT ach.achievement_id from achievements ach WHERE ach.player_id = {playerId};";

			var package = new Package(EnumPackCommand.SC_GetAchievements_Ret);

			var info = conn.Query<int>(sql).ToList();

			if (info.Count > 0)
			{
				//0表示成功，其余是失败
				package.Write((byte)EnumReqRetStatus.Success);
				package.Write(info.Count);
				for (int i = 0; i < info.Count; i++)
				{
					package.Write(info[i]);
				}
			}
			else
			{
				//非0是失败
				package.Write((byte)EnumReqRetStatus.Fail0);
			}

			session.Send(package);
			Program.ConnectionPool.returnConnection(conn);//连接用完回收
		}


	}
}
